package com.claude.mainactivity;

import com.claude.ui.CheckCompanet.MessageItem;
import com.claude.util.TimeTool;
import com.special.ResideMenuDemo.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class InitNewCardActivity extends Activity{
	private String mCardMac = null; 
	private int mItemResource = R.layout.my_list_item;
	private MyApplication mMyApplication = null;
	private ListView mListView = null;
	private ArrayAdapter<MessageItem> mArrayAdapter = null;
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_initcard);
		
		mCardMac = getIntent().getStringExtra("cardMac");
		mMyApplication = (MyApplication) getApplication();
		
		initView();
	}
	
	public void onStop(){
		super.onStop();
		this.finish();
	}
	
	private void initView(){
		mArrayAdapter = new ArrayAdapter<MessageItem>(InitNewCardActivity.this, mItemResource, mMyApplication.getCheckState());
		mListView = (ListView) findViewById(R.id.listview);
		mListView.setAdapter(mArrayAdapter);
		mListView.setOnItemClickListener(new MyOnItemClickListener());
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event){
		switch(keyCode){
		case KeyEvent.KEYCODE_BACK:
			showExitDialog();
			break;
		}
		return false;
	}
	
	private void gotoCheckStudent(){
		Intent intent = new Intent();
		intent.setClass(InitNewCardActivity.this, CheckStudentActivity.class);
		InitNewCardActivity.this.startActivity(intent);
	}
	
	void showExitDialog(){
		new AlertDialog.Builder(InitNewCardActivity.this)
		.setMessage("确定要退出更新新卡界面?")
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				gotoCheckStudent();
			}
		})
		.setNegativeButton("取消", null).create().show();
	}
	
	class MyOnItemClickListener implements OnItemClickListener{
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			MessageItem messageItem = mMyApplication.getCheckState().get(arg2);			
			String studentNumber = messageItem.time;
			mMyApplication.getCheckState().remove(arg2);
			int index = 0;
			while(index != mMyApplication.getStudents().size()){
				if(mMyApplication.getStudents().get(index).getStudentNumber().equals(studentNumber)){
					mMyApplication.getStudents().get(index).setCardMac(mCardMac);
					//mTmpCheckTableManager.insert(mSequenceId, tmp.time, "正常考勤", TimeTool.getTime().toString(), TimeTool.getDate().toString(), cardMac);
				}
				index++;
			}
			gotoCheckStudent();
		}
	}
}
